package vn.vfriends.soffice.bean;

import java.io.Serializable;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.enterprise.context.Conversation;
import javax.enterprise.context.ConversationScoped;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import javax.inject.Inject;
import javax.inject.Named;
import org.primefaces.context.RequestContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import vn.vfriends.soffice.model.Dept;
import vn.vfriends.soffice.service.DeptService;

/**
 *
 * @author chaungoctuan@gmail.com
 */
@Named
@ConversationScoped
public class DeptBean implements Serializable {

    private final Logger logger = LoggerFactory.getLogger(DeptBean.class);
    
    @Inject 
    private DeptService deptService;
    
    @Inject private Conversation conversation;
    private Dept[] selectedDepts;
    private Dept dept;
    private List<Dept> deptList;
    
    @PostConstruct
    public void init(){
        deptList = deptService.findAll();
    }
    
    public List<Dept> getDeptList() {
        return deptList;
    }

    public Conversation getConversation() {
        return conversation;
    }

    public Dept[] getSelectedDepts() {
        return selectedDepts;
    }

    public void setSelectedDepts(Dept[] selectedDepts) {
        this.selectedDepts = selectedDepts;
    }

    public Dept getDept() {
        return dept;
    }

    public void setDept(Dept dept) {
        this.dept = dept;
    }
    
    public void preNewAction() {
        dept = new Dept();
        
        if (conversation.isTransient()) {
            conversation.begin();
        }
    }
    
    public String saveAction() {
        try {
            this.deptService.create(this.dept);
            conversation.end();
            return "list?faces-redirect=true";
        } catch (Exception ex) {
            this.logger.error(ex.getMessage(), ex);
            FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_ERROR, "Thông báo", "Lỗi hệ thống");
            RequestContext.getCurrentInstance().showMessageInDialog(message);
            return null;
        }
    }
    
    public String cancelAction() {
        conversation.end();
        return "list?faces-redirect=true";
    }
    
    public String preEditAction() {
        String outcome = null;
        try {
            if (conversation.isTransient()) {
                conversation.begin();
            }
            
            if(this.dept != null){
                outcome = "edit?faces-redirect=true";
            } else if (this.selectedDepts == null || this.selectedDepts.length == 0) {
                FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_INFO, "Thông báo", "Hãy chọn một phòng ban");
                RequestContext.getCurrentInstance().showMessageInDialog(message);
            } else if (this.selectedDepts.length > 1) {
                FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_INFO, "Thông báo", "Chỉ chọn một phòng ban");
                RequestContext.getCurrentInstance().showMessageInDialog(message);
            } else {
                this.dept = this.selectedDepts[0];
                outcome = "edit?faces-redirect=true";
            }
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_ERROR, "Thông báo", "Lỗi hệ thống");
            RequestContext.getCurrentInstance().showMessageInDialog(message);
        }
        return outcome;
    }
    
    public String updateAction() {
        try {
            this.deptService.edit(dept);
            conversation.end();
            return "list?faces-redirect=true";
        } catch (Exception ex) {
            this.logger.error(ex.getMessage(), ex);
            FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_ERROR, "Thông báo", "Lỗi hệ thống");
            RequestContext.getCurrentInstance().showMessageInDialog(message);
            return null;
        }
    }
    
    public void preDeleteAction() {
        try {
            if (conversation.isTransient()) {
                conversation.begin();
            }
            if ((this.dept == null) && (this.selectedDepts == null || this.selectedDepts.length == 0)) {
                RequestContext.getCurrentInstance().addCallbackParam("invalid", true);
                
                FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_INFO, "Thông báo", "Hãy chọn một hoặc nhiều phòng ban");
                RequestContext.getCurrentInstance().showMessageInDialog(message);
            }
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_ERROR, "Thông báo", "Lỗi hệ thống");
            RequestContext.getCurrentInstance().showMessageInDialog(message);
        }
    }
    
    public void deleteAction() {
        try {
            if(this.dept != null) {
                this.deptService.remove(dept);
            }
            else {
                for(Dept selectedDept : selectedDepts) {
                    this.deptService.remove(selectedDept);
                }
            }
            if(!conversation.isTransient()) {
                conversation.end();
            }
            FacesContext.getCurrentInstance().getExternalContext().redirect("list.jsf");
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_ERROR, "Thông báo", "Lỗi hệ thống");
            RequestContext.getCurrentInstance().showMessageInDialog(message);
        }
    }
}
